/*
 * RiverFrame.java
 * Team qq 2011
 */
package com.google.code.timetrail.gui;

import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Component;
import javax.swing.JRadioButton;

import com.google.code.timetrail.backend.Control;
import com.google.code.timetrail.backend.Event;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class RiverFrame extends JPanel {
	
	private RiverFrameBackend riverFrameBackend;
	
	private JLabel titleText;
	
	private JLabel optionsText;
	
	private JRadioButton ferryRadioButton;
	
	private JRadioButton caulkRadioButton;
	
	private JRadioButton fordRadioButton;
	
	private final ButtonGroup optionsGroup = new ButtonGroup();
	
	private JButton confirmButton;
	
	private final Control gameControl;
	
	public RiverFrame(Control gC) {
		this.gameControl = gC;
		riverFrameBackend = new RiverFrameBackend(gC);
		setLayout(null);
		
		titleText = new JLabel(riverFrameBackend.riverStatus());
		titleText.setAlignmentX(Component.CENTER_ALIGNMENT);
		titleText.setBounds(21, 43, 366, 34);
		add(titleText);
		
		optionsText = new JLabel(riverFrameBackend.optionsText());
		optionsText.setBounds(151, 88, 90, 14);
		add(optionsText);
		
		ferryRadioButton = new JRadioButton(riverFrameBackend.ferryText());
		if(!(gameControl.getInv().canRemoveItem(riverFrameBackend.getRiver().getFerryPrice(), 
				gameControl.getInv().getMoney()))){
				optionsGroup.add(ferryRadioButton);
				ferryRadioButton.setBounds(138, 109, 169, 23);
				add(ferryRadioButton);
		}
		
		caulkRadioButton = new JRadioButton(riverFrameBackend.caulkText());
		caulkRadioButton.setSelected(true);
		optionsGroup.add(caulkRadioButton);
		caulkRadioButton.setBounds(138, 135, 169, 23);
		add(caulkRadioButton);
		
		fordRadioButton = new JRadioButton(riverFrameBackend.fordText());
		optionsGroup.add(fordRadioButton);
		fordRadioButton.setBounds(138, 161, 116, 23);
		add(fordRadioButton);
		
		confirmButton = new JButton(riverFrameBackend.confirmText());
		confirmButton.setEnabled(false);
		confirmButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent action) {
				if(ferryRadioButton.isSelected()) {
					gameControl.setNextEvent(new Event(riverFrameBackend.getRiver().takeFerry()));
					//Button SHOULD NOT APPEAR if you don't got the money
				}
				else if(caulkRadioButton.isSelected()){
					gameControl.setNextEvent(new Event(riverFrameBackend.getRiver().caulk()));
				}
				else if(fordRadioButton.isSelected()){
					gameControl.setNextEvent(new Event(riverFrameBackend.getRiver().ford()));
				}
				else{
					gameControl.setNextEvent(new Event(riverFrameBackend.errorText()));
					
				}
			}
		});
		confirmButton.setBounds(134, 191, 89, 23);
		add(confirmButton);
	}
}
